package pe.gob.foncodes.ssa.bs.common.util;

import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

public class MessageResourcesUtil {

    public static String CONFIG_FILE_NAME = "pe/gob/foncodes/ssa/conf/message.conf.properties";
    public static String BASE_NAME = "base.name";
    private static Properties prop;
    private static Map<String, MessageResources> resourceInstances = new HashMap<String, MessageResources>();
    private static MessageResources defaultResource;
    private static Logger logger = Logger.getRootLogger();
    
    // Initialize the bundle object
    static {
        init();
    }
    
    private static void init() {
        loadConfiguration(null);
        loadDefaultLocalBundle();

    }

    /**
     * Initialize MessageResourceUtil with configuration properties file
     * 
     * @param properties
     */
    /**
     * @param properties
     */
    public static void init(Properties properties) {
        try {
            loadConfiguration(properties);
            loadDefaultLocalBundle();
        } catch (Exception e) {
            logger.error("Could not load properties configuration file ", e);
        }

    }

    /**
     * Returns the message resource for this locale.
     * 
     * @param locale
     * @return
     */
    /**
     * @param locale
     * @return
     */
    public static MessageResources getResource(Locale locale) {
        MessageResources resource = null;
        if (resourceInstances.containsKey(locale.toString())) {
            return resourceInstances.get(locale.toString());
        } else {
            // creating and caching avaliable resource bundles
            String baseName = prop.getProperty(BASE_NAME);
            loadBundle(baseName, locale);
            resource = getResource(locale);
        }
        return resource;
    }

    /**
     * Returns the default message resource file
     * 
     * @return
     */
    /**
     * @return
     */
    public static MessageResources getDefault() {
        return defaultResource;
    }

    /**
     * Loads an bundle file
     * 
     * @param baseName
     * @param localeString
     */
    /**
     * @param baseName
     * @param locale
     */
    private static void loadBundle(String baseName, Locale locale) {
        resourceInstances.put(locale.toString(), new MessageResources(
                ResourceBundle.getBundle(baseName, locale)));
    }

    /**
     * 
     */
    private static void loadDefaultLocalBundle() {
        ResourceBundle bundle = ResourceBundle.getBundle(
                prop.getProperty(BASE_NAME), new Locale(""));
        defaultResource = new MessageResources(bundle);
    }

    /**
     * Loads configuration.
     * 
     * @param properties
     *            if pass null, default configuration is used.
     */
    /**
     * @param properties
     */
    private static void loadConfiguration(Properties properties) {
        if (prop == null) {
            try {
                prop = new Properties();
                URL resource = MessageResources.class.getClassLoader().getResource(CONFIG_FILE_NAME);
                prop.load(resource.openStream());
            } catch (Exception e) {
                logger.info("Could not load default configuration file in classpath: "+ CONFIG_FILE_NAME);
                logger.debug("Exception: ", e);
            }
        } else {
            prop = properties;
        }
    }
}